package com.zxc.controller.admin;

import com.zxc.model.Course;
import com.zxc.model.Exam;
import com.zxc.model.Student;
import com.zxc.model.Teacher;
import com.zxc.service.CourseService;
import com.zxc.service.ExamService;
import com.zxc.service.PageService;
import com.zxc.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("admin")
public class AdminController {
    @Resource
    private UserService userService;

    @Resource
    private PageService pageService;

    @Resource
    private CourseService courseService;

    @Resource
    private ExamService examService;

    @RequestMapping("/adminIndex")
    public String studentIndex(){
        return "admin/adminIndex";
    }

    @RequestMapping("/studentList")
    public String userList(@Param("page") int page, Model model) {
        List<Student> studentList = userService.queryAllStudent();
        model.addAttribute("paging", pageService.subList(page, 6, studentList));
        return "/admin/studentList";
    }
    @RequestMapping("/examList")
    public String examList(@Param("page") int page, Model model) {
        List<Course> courseList = courseService.queryAllCourse();
        model.addAttribute("paging", pageService.subList(page, 6, courseList));
        return "/admin/examList";
    }
    @RequestMapping("/examDetail")
    public String examDetail(@Param("courseid") int courseid, Model model) {
        Exam exam = examService.selectExamByClassId(courseid);
        model.addAttribute("exam", exam);
        return "/admin/examDetail";
    }
    @RequestMapping(value = "/insertExam", method = RequestMethod.GET)
    public String insertExam(@Param("courseid") int courseid, Model model) {
        Course course = courseService.queryCourse(courseid);
        model.addAttribute("course", course);
        return "/admin/insertExam";
    }
    @RequestMapping(value = "/insertExam", method = RequestMethod.POST)
    public String insertExamPost(Exam exam, @Param("classId") String classId, Model model) {
        exam.setClassid(Integer.parseInt(classId));
        examService.inserExam(exam);
        return "redirect:/admin/examList?page=1";
    }
    @RequestMapping("/teacherList")
    public String teacherList(@Param("page") int page, Model model) {
        List<Teacher> teacherList = userService.queryAllTeacher();
        model.addAttribute("paging", pageService.subList(page, 6, teacherList));
        return "/admin/teacherList";
    }
    @RequestMapping("changePass")
    public String changePass(@Param("password") String password, Model model) {
        Boolean flag = userService.changeAllPassword(password);
        if (flag) {
            return "redirect:/admin/studentList?page=1";
        } else {
            return "/admin/failed";
        }
    }

    @RequestMapping("teacherChangePass")
    public String teacherChangePass(@Param("password") String password, Model model) {
        Boolean flag = userService.changeTeacherAllPassword(password);
        if (flag) {
            return "redirect:/admin/teacherList?page=1";
        } else {
            return "/admin/failed";
        }
    }


}
